package cn.edu.scau.nursingcenter.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.nursingcenter.bean.ResultBean;
import cn.edu.scau.nursingcenter.entiry.Rehabilitation;
import cn.edu.scau.nursingcenter.service.impl.RehabilitationServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 康复计划控制类RehabilitationController 所有方法统一返回JSON数据
 * @author feng
 *
 */
@Api(tags = "康复计划控制类")
@RestController
@RequestMapping(value = "rehabilitation")
public class RehabilitationController {

	@Autowired
	private RehabilitationServiceImpl rehabilitationServiceImpl;
	/**
	 * 显示所有计划信息
	 * @return
	 */
	@ApiOperation(value = "显示所有计划信息")
	@ResponseBody
	@GetMapping(value = "/list")
	public ResultBean list() {
		ResultBean resultBean = null;
		List<Rehabilitation> rehabilitations = rehabilitationServiceImpl.selectAll();
		if(rehabilitations.size() != 0) {
			resultBean = new ResultBean(200,true,"显示所有计划成功",rehabilitations);
		}else {
			resultBean = new ResultBean(5009,false,"显示所有计划失败",null);
		}
		return resultBean;
	}
	/**
	  * 新增康复计划
	 * @param rehabilitation对象
	 * @return
	 */
	@ApiOperation(value = "新增康复计划")
	@ResponseBody
	@PostMapping(value = "/insert")
	public ResultBean insert(@Validated @RequestBody Rehabilitation rehabilitation) {
		ResultBean resultBean = null;
		Rehabilitation targetRehabilitation = rehabilitationServiceImpl.insert(rehabilitation);
		if(targetRehabilitation != null) {
			resultBean = new ResultBean(200,true,"新建康复计划成功",targetRehabilitation);
		}else {
			resultBean = new ResultBean(5008,false,"新建康复计划失败",null);
		}
		return resultBean;
	}
	
	/**
	 * 修改康复计划
	 * @param rehabilitation对象
	 * @return
	 */
	@ApiOperation(value = "修改康复计划")
	@ResponseBody
	@PostMapping(value = "/update")
	public ResultBean update(@Validated @RequestBody Rehabilitation rehabilitation) {
		ResultBean resultBean = null;
		Rehabilitation targetRehabilitation = rehabilitationServiceImpl.update(rehabilitation);
		if(targetRehabilitation != null) {
			resultBean = new ResultBean(200,true,"修改康复计划成功",targetRehabilitation);
		}else {
			resultBean = new ResultBean(5007,false,"修改康复计划失败",null);
		}
		return resultBean;
	}
	
	/**
	 *  根据客户姓名查询
	 * @param name客户姓名
	 * @return
	 */
	@ApiOperation(value = "根据客户姓名查询")
	@ApiImplicitParam(name = "name",value = "客户姓名",required = true, dataType = "String")
	@ResponseBody
	@GetMapping(value = "/find")
	public ResultBean findByName(@RequestParam(defaultValue = "")String name) {
		ResultBean resultBean = null;
		List<Rehabilitation>rehabilitations = rehabilitationServiceImpl.findByName(name);
		if(rehabilitations.size() != 0) {
			resultBean = new ResultBean(200, true, "查询成功", rehabilitations);
		}else {
			resultBean = new ResultBean(5006,false,"查询失败",null);
		}
		return resultBean;
	}
	/**
	 * 根据计划编号删除
	 * @param id康复编号
	 * @return
	 */
	@ApiOperation(value = "根据计划编号删除")
	@ApiImplicitParam(paramType = "path",name = "id",value = "编号",required = true,dataType = "int")
	@ResponseBody
	@DeleteMapping(value = "/delete/{id}")
	public ResultBean delete(@PathVariable Integer id) {
		ResultBean resultBean = null;
		if(id != null) {
			rehabilitationServiceImpl.delete(id);
			resultBean = new ResultBean(200,true,"删除成功",null);
		}else {
			resultBean = new ResultBean(5005,false,"康复计划不存在，请确认再删除",null);
		}
		return resultBean;
	}
}
